﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PO_2011
{
    public partial class Gimp : Form
    {
        public PNM_Reader img = null;
        private bool timer_data_start = true;
        DateTime start_time;

        public Gimp()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void gimpTimer()
        {
            if (this.timer_data_start)
            {
                this.start_time = DateTime.Now;
                this.timer_data_start = false;
            }
            else
            {
                DateTime stop_time = DateTime.Now;
                TimeSpan elapsed_time = stop_time - start_time;
                timerLabel.Text = elapsed_time.TotalSeconds.ToString("0.000000");
                this.timer_data_start = true;
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(openImage.ShowDialog() != DialogResult.Cancel)
            {
                this.gimpTimer();
                this.img = new PNM_Reader(openImage.FileName);

                imageBox.Width = this.img.Header.Width;
                imageBox.Height = this.img.Header.Height;
                imageBox.Image = this.img.bitmap;

                ImageHistory.History.addStep(this.img.bitmap);

                this.gimpTimer();

                this.enableButtons();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveImage.ShowDialog() != DialogResult.Cancel)
            {
                PopupFileType popup = new PopupFileType(this);
                popup.Show();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void imageBox_Click(object sender, EventArgs e)
        {
        }


        public byte[] StrToByteArray(string str)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            return encoding.GetBytes(str);
        }

        private void gaussaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            PopupFilter popup_filter = new PopupFilter(this, "gauss");
            popup_filter.Show();
        }

        private void edycjaCofnijToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap btm = ImageHistory.History.returnLastImage();

            if (btm != null)
            {
                imageBox.Image = this.img.bitmap = btm;
                imageBox.Refresh();
            }
        }

        private void jednoliteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            PopupFilter popup_filter = new PopupFilter(this, "jednolite");
            popup_filter.Show();
        }

        private void enableButtons()
        {
            edycjaToolStripMenuItem.Enabled = true;
            filtrToolStripMenuItem.Enabled = true;
            saveToolStripMenuItem.Enabled = true;
            efektyToolStripMenuItem.Enabled = true;
            timerLabel.Visible = true;
        }

        private void disableButtons()
        {
            edycjaToolStripMenuItem.Enabled = false;
            filtrToolStripMenuItem.Enabled = false;
            saveToolStripMenuItem.Enabled = false;
        }


        //ogólny filtr splotowy
        private void generalFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            PopupFilterInput popup_filter = new PopupFilterInput(this, "general");
            popup_filter.Show();
        }

        private void sobelToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Filter.Filter.create(new Bitmap(this.imageBox.Image), "sobel", 3);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("sobel");
        }

        private void previttaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Filter.Filter.create(new Bitmap(this.imageBox.Image), "previtt", 3);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("previtt");
        }

        private void robertsaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Filter.Filter.create(new Bitmap(this.imageBox.Image), "roberts", 2);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("roberts");
        }

        private void loGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));

            PopupFilter popup_filter = new PopupFilter(this, "gauss");
            popup_filter.filterGaussForLog();
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Filter.Filter.create(new Bitmap(this.imageBox.Image), "LoG", 9);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("LoG");
        }

        private void dogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));

            PopupFilter popup_filter = new PopupFilter(this, "gauss");
            popup_filter.filterGaussForLog();
            popup_filter.filterGaussForLog();
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Filter.Filter.create(new Bitmap(this.imageBox.Image), "LoG", 9);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("DoG");
        }

        private void erozjaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Noise.Noise.create(new Bitmap(this.imageBox.Image), "erozja", 3);
            
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Erozja");
        }

        private void dylacjaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = this.img.bitmap = Noise.Noise.create(new Bitmap(this.imageBox.Image), "dylacja", 3);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Dylacja");
        }

        private void otwarciaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));

            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Noise.Noise.create(new Bitmap(this.imageBox.Image), "erozja", 3);
            this.imageBox.Image = Noise.Noise.create(new Bitmap(this.imageBox.Image), "dylacja", 3);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Dylacja");
        }

        private void domknięciaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));

            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Noise.Noise.create(new Bitmap(this.imageBox.Image), "dylacja", 3);
            this.imageBox.Image = Noise.Noise.create(new Bitmap(this.imageBox.Image), "erozja", 3);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Dylacja");
        }

        private void odszumanieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Filter.Filter.create(new Bitmap(this.imageBox.Image), "odszumianie_mediana", 3);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Odszumianie mediana");
        }

        private void oilToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Effect.makeEffect(new Bitmap(this.imageBox.Image), "oil", 5);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Efekt olej");
        }

        private void negativeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Effect.makeEffect(new Bitmap(this.imageBox.Image), "negative");
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Efekt negative");
        }

        private void solarisationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Effect.makeEffect(new Bitmap(this.imageBox.Image), "solarisation");
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Efekt solarization");
        }

        private void leftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Effect.makeEffect(new Bitmap(this.imageBox.Image), "horizontal", 0);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Efekt horizontal left");
        }

        private void rightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Effect.makeEffect(new Bitmap(this.imageBox.Image), "horizontal", 1);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Efekt horizontal right");
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Effect.makeEffect(new Bitmap(this.imageBox.Image), "bathroom", 0);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Efekt bathroom 1");
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Effect.makeEffect(new Bitmap(this.imageBox.Image), "bathroom", 1);
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Efekt bathroom 2");
        }

        private void northToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Shadow.generate(new Bitmap(this.imageBox.Image), "north");
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Shadow north");
        }

        private void eastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Shadow.generate(new Bitmap(this.imageBox.Image), "east");
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Shadow east");
        }

        private void southToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Shadow.generate(new Bitmap(this.imageBox.Image), "south");
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Shadow south");
        }

        private void westToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = Effect.Shadow.generate(new Bitmap(this.imageBox.Image), "west");
            this.imageBox.Refresh();
            this.gimpTimer();
            MessageBox.Show("Shadow west");
        }

        private void cannyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gimpTimer();
            ImageHistory.History.addStep(new Bitmap(this.imageBox.Image));
            this.imageBox.Image = ColorConvert.Colors.convertBitmapColorToGreyOrBlack(new Bitmap(this.imageBox.Image), 1);
            this.imageBox.Image = Filter.Canny.create(new Bitmap(this.imageBox.Image), "gaussian", 5);
            this.imageBox.Image = Filter.Canny.create(new Bitmap(this.imageBox.Image), "sobel", 3);
            this.imageBox.Refresh();
            this.gimpTimer(); 
            MessageBox.Show("Canny");
        }

        private void houghToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }


    }
}
